package com.education.service.impl;

import java.util.*;

import com.education.model.QuestionModel2;
//import com.education.util.ExcelUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.education.dao.SelectExamDao;
import com.education.model.CourseModel;
import com.education.model.Judge;
import com.education.model.QuestionExam;
import com.education.service.SelectExamApi;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**李志鹏
 * @author Administrator
 *
 */
public class SelectExamService implements SelectExamApi {
    
    @Autowired
    private SelectExamDao sed;
    
    private static Logger logger= LogManager.getLogger(SelectExamService.class);
    
    
    /**
     * 2017/8/7 王娇
     * 选择题页面 分页
     * 返回题库的页面数据包括分页数据,下拉框数据
     * @return 返回页面数据
     * @throws Exception 
     */
    @Override
    public Map<Object, Object> getPage(Integer pageNo,Integer pageSize, String searchMessage,Integer questionType) throws Exception {
        /**
         * 2017/8/7 王娇
         * 查询教师子章节
         * 假设教师id为1
         *
         */
        int teacherId=1;

        Map<Object,Object> map = new HashMap<Object,Object>();
        List<QuestionModel2> listSection=sed.getCourseSection(teacherId);
        List<CourseModel> listCourse = sed.getCourse(teacherId);


        pageNo = pageNo == null?1:pageNo;
        pageSize = pageSize == null?3:pageSize;
        PageHelper.startPage(pageNo, pageSize);

        List<QuestionExam> listSelectPage=sed.getSelectPage(questionType,searchMessage);

        PageInfo<QuestionExam> page = new PageInfo<QuestionExam>(listSelectPage);
        map.put("page", page);
        map.put("listSection", listSection);
        map.put("listCourse", listCourse);
        return map;
    }
    /**
     * 添加章节选择题库
     * @throws Exception 
     */
    @Override
    public void addCourseSection(QuestionExam se) throws Exception {
        // TODO Auto-generated method stub
        se.setSelectAddtime(new Date());
        sed.addCourseSection(se);
    }
    /**
     * 添加期末选择题库
     * @throws Exception 
     */
    @Override
    public void addExamService(QuestionExam se) throws Exception {
        // TODO Auto-generated method stub
        se.setSelectAddtime(new Date());
        sed.addExam(se);
    }
    /**
     * 添加入学选择题库
     * @throws Exception 
     */
    @Override
    public void addEnterText(QuestionExam se) throws Exception {
        // TODO Auto-generated method stub
        se.setSelectAddtime(new Date());
        sed.addEnter(se);
    }
    /**
     * 更新选择题库
     * @throws Exception 
     */
    @Override
    public void update(QuestionExam se) throws Exception {
        // TODO Auto-generated method stub
        se.setModifyDate(new Date());
        se.setModifyPerson("李志鹏");
        sed.updateSelect(se);
    }
    /**
     * 删除选择题库
     * @throws Exception 
     */
    @Override
    public void delete(Integer selectId) throws Exception {
        // TODO Auto-generated method stub
        sed.deleteSelect(selectId);

    }

    /**
     * 批量导入
     * @param
     * @throws Exception
     */
    public void excelUplode(Map<String, ArrayList<String>> map,QuestionExam qe) throws Exception {

        Set<String> set=map.keySet();
        Iterator<String> iterator=set.iterator();
        List<QuestionExam> list=new ArrayList<QuestionExam>();
        while(iterator.hasNext()){
            String key=iterator.next();
            ArrayList<String> arrayList=map.get(key);
            QuestionExam qe1=new QuestionExam();
            if (arrayList.size()==8){
                for (int a=0;a<arrayList.size();a++) {

                    qe1.setQuestionName(arrayList.get(0));
                    qe1.setSelectA( arrayList.get(1));
                    qe1.setSelectB( arrayList.get(2));
                    qe1.setSelectC( arrayList.get(3));
                    qe1.setSelectD(arrayList.get(4));
                    qe1.setSelectAnswer( arrayList.get(5));
                    qe1.setSelectPoint(Integer.parseInt(arrayList.get(6)));
                    qe1.setQuestionDifficulty(Integer.parseInt(arrayList.get(7)));
                    qe1.setTeacherId(qe.getTeacherId());
                    qe1.setScoreType(qe.getScoreType());
                    qe1.setCourseId(qe.getCourseId());
                    qe1.setSelectAddtime(new Date());
                    qe1.setSectionId(qe.getSectionId());
                    qe1.setQuestionType(qe.getQuestionType());

                }
                list.add(qe1);
            }

            if(arrayList.size()==6){
                for(int a=0;a<arrayList.size();a++){
                    qe1.setQuestionName(arrayList.get(0));
                    qe1.setSelectA( arrayList.get(1));
                    qe1.setSelectB( arrayList.get(2));
                    qe1.setSelectAnswer( arrayList.get(3));
                    qe1.setSelectPoint(Integer.parseInt(arrayList.get(4)));
                    qe1.setQuestionDifficulty(Integer.parseInt(arrayList.get(5)));
                    qe1.setTeacherId(qe.getTeacherId());
                    qe1.setScoreType(qe.getScoreType());
                    qe1.setCourseId(qe.getCourseId());
                    qe1.setSelectAddtime(new Date());
                    qe1.setSectionId(qe.getSectionId());
                    qe1.setQuestionType(qe.getQuestionType());
                }
                list.add(qe1);
            }

        }

        sed.excelUplode(list);
    }
}
